package Sockets;



import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Alejandro Figueroa
 */
public class lecturaXML {

    List petroleoEntradas;
    Document dom;
    
    public lecturaXML(){
        //Se crea una lista para meter las entradas de petroleo
        petroleoEntradas = new ArrayList();
    }

    public void lecturaXmlFile(){

        //get the factory
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

	try{

            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file
            dom = db.parse("EntradasPetroleo.xml");


	}catch(ParserConfigurationException pce){
            pce.printStackTrace();
            System.out.println("Error con la configuracion del archivo XML");

	}catch(SAXException se){
            se.printStackTrace();
            System.out.println("Error con SAX");

	}catch(IOException ioe){
            ioe.printStackTrace();
            System.out.println("Error IOE");
            
	}
    }

    public List lecturaDocumento(){

        //obtengo el elemento raiz (Tanque)
	Element docEle = dom.getDocumentElement();

	//obtengo una lista de nodos de elementos tanque
	NodeList listaItem = docEle.getElementsByTagName("tanque");

        if(listaItem != null && listaItem.getLength() > 0) {

            for(int i = 0 ; i < listaItem.getLength();i++) {

		//obtengo el elemento empleado
		Element item = (Element)listaItem.item(i);

		//obtengo el objeto empleado
		XmlTanque e = getEntrada(item);

		//add it to list
		petroleoEntradas.add(e);
            }

	}

        return petroleoEntradas;

    }

    public XmlTanque getEntrada(Element entradaItem) {

        int id = getValor(entradaItem,"id");
	int litrosEntrada = getValor(entradaItem,"lts");

        //Create a new Employee with the value read from the xml nodes
        XmlTanque e = new XmlTanque(id,litrosEntrada);

	return e;
    }

    private int getValor(Element ele, String tagName) {

        int valor = 0;
        NodeList nl = ele.getElementsByTagName(tagName);

        if(nl != null && nl.getLength() > 0) {

            Element el = (Element)nl.item(0);

            valor = Integer.parseInt(el.getFirstChild().getNodeValue());
        }
		
        return valor;

    }

    private void printData(){
		
        System.out.println("Entradas = " + petroleoEntradas.size());

	Iterator it = petroleoEntradas.iterator();

	while(it.hasNext()) {

            System.out.println(it.next().toString());

	}

    }

    public List runLectura() {

	//parse the xml file and get the dom object
	lecturaXmlFile();

	//get each employee element and create a Employee object
	return lecturaDocumento();

	//Iterate through the list and print the data
	//printData();

    }

}